<?php
namespace Home\Controller;
use Think\Controller;
class PublicController extends Controller{
	public function __construct(){
		parent::__construct();
		$this->checkLogin();//验证是否登录
		if(!S('scate')){
			$this->loginPower();//验证权限
		}
	}
	
	//验证是否登录
	public function checkLogin() {
		if(empty($_SESSION['my_info'])){ 
		    $this->redirect("Login/login");
		}
		return true;
	}
//验证权限
	public function loginPower(){
	   
		$url=$_SERVER['QUERY_STRING'];
		$u = explode('&',$url); 
		foreach($u as $vu){
			if(strstr($vu,'c=')){
				$bigcate=end(explode('=',$vu));
			}elseif(strstr($vu,'a=')){
				$smallcate=end(explode('=',$vu));
			}
		}
		$bcate = $_SESSION['my_info']['bigcate'];
		$scate = $_SESSION['my_info']['smallcate']; 
		$b = explode(',',$bcate);
		$s = explode(',',$scate);
		$b = is_array($b)?$b:$bcate;
		$s = is_array($s)?$s:$scate;
		foreach($s as $v){
			$ss[]=end(explode('-',$v));
		} 
		
		$temp=array();
		$rightlist=M("smallcategroy")->field("smallaction")->select();
		if($rightlist){
		    foreach ($rightlist as $myvalue){
		        $temp[]=$myvalue["smallaction"];
		    }
		}else{
		    $this->error("系统错误","Login/index");
		}
		if(in_array($smallcate,$temp)){
		    if((!in_array($bigcate,$b)) || ($bcate == $bigcate)) {
		        if(!in_array($smallcate,$ss)){
		            $this->error('此账号权限不足！',U("Login/index"));
		        }
		    }elseif((!in_array($smallcate,$ss)) || ($scate == $smallcate)){
		        $this->error('此账号权限不足！',U("Login/index"));
		    }
		}
		foreach($s as $t){
			$a=stripos($t,'-');
			$tt[]=substr($t,0,$a);
		}
		if(is_array($s)){
			S('sacate',$tt);
		}else{
			$aa=stripos($scate,'-');
			$tt=substr($t,0,$aa);
			S('sacate',$tt);
		}
		S('bcate',$b);
		S('scate',$s);
		return true;
	}
	
	
	
	
	//获取IP地址城市
	public function GetIpLookup($ip = ''){
		if(empty($ip)){
			if ($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"])
			{
				$ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];
			}
			elseif ($HTTP_SERVER_VARS["HTTP_CLIENT_IP"])
			{
				$ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];
			}
			elseif ($HTTP_SERVER_VARS["REMOTE_ADDR"])
			{
				$ip = $HTTP_SERVER_VARS["REMOTE_ADDR"];
			}
			elseif (getenv("HTTP_X_FORWARDED_FOR"))
			{
				$ip = getenv("HTTP_X_FORWARDED_FOR");
			}
			elseif (getenv("HTTP_CLIENT_IP"))
			{
				$ip = getenv("HTTP_CLIENT_IP");
			}
			elseif (getenv("REMOTE_ADDR"))
			{
				$ip = getenv("REMOTE_ADDR");
			}
			else
			{
				$ip = "Unknown";
			}
		}
		$res = @file_get_contents('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=' . $ip);
		if(empty($res)){ return false; }
		$jsonMatches = array();
		preg_match('#\{.+?\}#', $res, $jsonMatches);
		if(!isset($jsonMatches[0])){ return false; }
		$json = json_decode($jsonMatches[0], true);
		if(isset($json['ret']) && $json['ret'] == 1){
			$json['ip'] = $ip;
			unset($json['ret']);
		}else{
			return false;
		}
		return $json;
	}
	
	
	
	/**
	 * 
	 * @param unknown 模型
	 * @param unknown $where
	 * @param number $pagecount
	 * @param string $order
	 * @param string $join
	 * @param string $join1
	 * @return unknown
	 */
	public function page($obj,$where,$pagecount=10,$order=null,$join=null,$join1=null,$order){
		$User = $obj; // 实例化User对象
		$count      = $User->where($where)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,$pagecount);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		$list = $User->where($where)->join($join)->join($join1)->order($order)->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach($list as $k => $v){
			if(!empty($v['zuiip'])){
				$ip = $v['zuiip'];
				$dz = $this->GetIpLookup($ip);
				$list[$k]['zuiip'] = $dz['ip'].$dz['city'];
			}			
		}
		$this->assign('list',$list);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		$this->assign('count',$count);//赋值总条数
		return $list;
	}
	
	
	
}